import {Component,Output,Input,EventEmitter} from '@angular/core';
import {AlertController, IonicPage, NavController, NavParams, ToastController, Platform, ItemSliding} from 'ionic-angular';

import {
    NoticeItem, RepeatItem, ScheduleItem,
    ScheduleStrResourceCh, TimerItem
} from "../../../model/pages/schedule/schedule-add/schedule-add.model";
import {ScheduleAddProvider} from "../../../providers/schedule/schedule-add/schedule-add.provider";
import * as moment from "moment";
import {ToastService} from '../../../services/common/toast.service';

@Component({
    selector: 'adp-relate-schedule',
    templateUrl: 'adp-relate-schedule.html',
    providers: [
        ScheduleAddProvider,
        ToastService
    ]
})
export class AdpRelateScheduleComponent {
    // 输出日程数据
    @Output() scheduleTigger:EventEmitter<any>=new EventEmitter();
    // 结束时间
    @Input() endRepeat:any;
    // 是否关联到日程
    @Input() addSchedule: boolean;

    // 是否可以二次编辑
    public editStatus: boolean = true;
    // 日程事项
    private data: ScheduleItem = new ScheduleItem();
    // 时间选择字符串
    private timerStr: TimerItem = new TimerItem();
    // 最小开始时间
    private minStartTime: string;
    // 最大开始时间
    private maxStartTime: string;
    // 最小结束时间
    private minEndTime: string;
    // 最大结束时间
    private maxEndTime: string;
    // 国际时间差值
    private differTime: number = 8 * 60 * 60 * 1000;
    // 显示的开始时间
    private startTime: string;
    // 显示的结束时间
    private endTime: string;
    // 重复
    private repeatItem = RepeatItem;
    // 提醒
    private noticeItem = NoticeItem;
    // 页面提示
    private curStrResource = ScheduleStrResourceCh;
    // 当前编辑文字长度
    private editingLang: number;
    // 列表时间戳
    private firstUnix: number;
    // 时间组件
    private pickerOptions = {
        cssClass: 'schedule-add-picker'
    };
    //endRepeat: any = '永不';//结束重复时间:永不 or UNIX时间戳
    // 开始时间年度范围
    private startYearList: number[] = [];
    // 结束时间年度范围
    private endYearList: number[] = [];

    private titleLengthErr:boolean;

    constructor(private scheduleAddProvider: ScheduleAddProvider,
                private navCtrl:NavController,
                private toastService:ToastService
                ) {

    }

    ngOnInit(){
        this.initScheduleNew(moment(new Date(moment().format('YYYY-MM-DD'))).unix()*1000-8*60*60*1000);
        this.startYearList=this.scheduleAddProvider.getYearList(2010);
        this.endYearList=this.scheduleAddProvider.getYearList(moment(new Date()).year());
        this.scheduleTigger.emit(this.data);
    }

    // 新建页初始化数据
    initScheduleNew(date: number) {
        const startTime = this.scheduleAddProvider.timeFormat(date, true);
        const endTime = this.scheduleAddProvider.timeFormat(date + 60 * 60 * 1000, true);
        if (!startTime || !endTime) {
            return false;
        }
        this.data.startTime = new Date(startTime.showTime).getTime();
        this.timerStr.startTimeStr = startTime.ionTime;
        this.data.endTime = new Date(endTime.showTime).getTime();
        this.timerStr.endTimeStr = endTime.ionTime;
        this.data.frequency = 1;
        this.data.reminder = 1;
        this.editStatus = true;
        this.maxEndTime = this.scheduleAddProvider.limitTimeRange(this.data.startTime).maxTime;
        this.editingLang = 0;
    }

    // 开始时间选择
    onStartTimeChange() {
        this.data.startTime = new Date(this.timerStr.startTimeStr).getTime() - this.differTime;
        const startLimit = this.scheduleAddProvider.limitTimeRange(this.data.startTime);
        this.maxEndTime = startLimit.maxTime;
        this.data.endTime = startLimit.unixTime;
        this.timerStr.endTimeStr = startLimit.ionTime;
        this.endYearList=this.scheduleAddProvider.getYearList(moment(new Date(this.data.startTime)).year());
        this.scheduleTigger.emit(this.data);
    }

    // 结束时间选择
    onEndTimeChange() {
        this.data.endTime = new Date(this.timerStr.endTimeStr).getTime() - this.differTime;
        this.scheduleTigger.emit(this.data);
    }

    // 其他日程数据change
    changeData(){
        this.scheduleTigger.emit(this.data);
    }

    // 跳转到结束重复页面
    openEndRepeat(endTimeStr) {
        this.navCtrl.push('ScheduleEndRepeatPage', {endTime: endTimeStr, endRepeat: this.endRepeat})
    }

    /**
     * 显示结束重复的时间
     * @param endRepeat '永不'或者时间戳
     * @returns {string}
     */
    getEndRepeatTime(endRepeat) {
        if (endRepeat === '永不') {
            return '永不'
        } else {
            return moment(endRepeat * 1000).format('YYYY年MM月DD日')
        }
    }

    // 标题编辑
    editTitle($event){
        if($event.target.value.length>=30&&!this.titleLengthErr){
            this.titleLengthErr=true;
            this.toastService.showToast('日程标题长度不能大于30')
        }
    }

    blurTitle(){
        this.titleLengthErr=false;
    }

}
